#include "BaseType.h"
#include "Tracer.h"
using namespace std;
using namespace libemb;

/**
 * 策略模式：
 * 1.将对象的行为进行抽象成策略，策略与对象解耦
 * 2.对象可以动态地改变策略
 * 3.和状态模式的区别：
 * 适用场景：
 * 1.
 * 2.
 **/

class Strategy{
DECL_CLASSNAME(Strategy)
public:
    virtual int calc(int x, int y)=0;
};

//加法策略
class StrategyAdd:public Strategy{
DECL_CLASSNAME(StrategyAdd)
public:
    int calc(int x, int y) override
    {
        return x+y;
    }
};

//减法策略
class StrategySub:public Strategy{
DECL_CLASSNAME(StrategySub)
public:
    int calc(int x, int y) override
    {
        return x-y;
    }
};

//上下文
class Context{
DECL_CLASSNAME(Context)
private:
    std::shared_ptr<Strategy> m_strategy;
public:
    Context(std::shared_ptr<Strategy> strategy)
    {
        m_strategy = strategy;
    }
    int getResult(int x, int y)
    {
        return m_strategy->calc(x,y);
    }
};

//不同的策略产生不同的结果
int main()
{
    Tracer::getInstance().setLevel(TRACE_LEVEL_DBG);
	Tracer::getInstance().addSink(std::make_shared<STDSink>()).start();

    int x = 10;
    int y = 5;
    auto context = std::make_shared<Context>(std::make_shared<StrategyAdd>());
    TRACE_INFO("StrategyAdd: %d + %d = %d",x,y,context->getResult(x,y));

    context = std::make_shared<Context>(std::make_shared<StrategySub>());
    TRACE_INFO("StrategySub: %d - %d = %d",x,y,context->getResult(x,y));

    return RC_OK;
}